package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.EYSystem.dao.BedDAO;
import com.neusoft.EYSystem.po.Bed;
import com.neusoft.EYSystem.po.BedInfo;
import com.neusoft.EYSystem.po.ResponseBean;
import com.neusoft.EYSystem.service.BedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bed")
public class BedController {
    @Autowired
    private BedService bedService;

    @Autowired
    private BedDAO bedDAO;

    @GetMapping("/listAll")
    public ResponseBean<List<Bed>> listAllBeds() {
        List<Bed> beds = bedDAO.selectList(null);
        ResponseBean<List<Bed>> response = new ResponseBean<>();
        if (beds != null && !beds.isEmpty()) {
            response.setStatus(200);
            response.setMsg("床位列表查询成功");
            response.setData(beds);
        } else {
            response.setStatus(404);
            response.setMsg("没有找到床位信息");
        }
        return response;
    }

    @GetMapping("/getBedById")
    public ResponseBean<Bed> getBedById(Integer bedId) {
        Bed bed = bedDAO.selectById(bedId);
        ResponseBean<Bed> response = new ResponseBean<>();
        if (bed != null) {
            response.setStatus(200);
            response.setMsg("床位信息查询成功");
            response.setData(bed);
        } else {
            response.setStatus(404);
            response.setMsg("没有找到对应的床位信息");
        }
        return response;
    }

    @GetMapping("/getBedNum")
    public ResponseBean<HashMap<String, Integer>> getBedNum(String floorNumber) {
        return bedService.getBedNum(floorNumber);
    }

    @GetMapping("/getList")
    public ResponseBean<List<Bed>> getList(String floorNumber){
        QueryWrapper<Bed> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("floor_number", floorNumber);
        List<Bed> beds = bedDAO.selectList(queryWrapper);
        ResponseBean<List<Bed>> response = new ResponseBean<>();
        if (beds != null && !beds.isEmpty()) {
            response.setStatus(200);
            response.setMsg("床位列表查询成功");
            response.setData(beds);
        } else {
            response.setStatus(404);
            response.setMsg("没有找到对应的病床信息");
        }
        return response;
    }

    @GetMapping("/getBedInfo")
    public ResponseBean<BedInfo> getBedInfo(Integer bedId){
        return bedService.getBedInfo(bedId);
    }

    @GetMapping("/getFreeNum")
    public ResponseBean<Map<String,List<Bed>>> getFreeNum() {
        QueryWrapper<Bed> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "free");
        List<Bed> freeBeds = bedDAO.selectList(queryWrapper);
        ResponseBean<Map<String,List<Bed>>> response = new ResponseBean<>();
        response.setData(new HashMap<>());
        if (freeBeds != null && !freeBeds.isEmpty()) {
            for(Bed bed : freeBeds){
                String roomNum = bed.getRoomNumber();
                if(!response.getData().containsKey(roomNum)){
                    response.getData().put(roomNum, new ArrayList<>());
                }
                response.getData().get(roomNum).add(bed);
            }
            response.setStatus(200);
            response.setMsg("空闲床位查询成功");
        } else {
            response.setStatus(404);
            response.setMsg("没有找到空闲的床位");
            response.setData(new HashMap<>());
        }
        return response;
    }
}
